//
//  RetrievePassworkViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/25.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "RetrievePassworkViewController.h"

@interface RetrievePassworkViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *accountFireld;
@property (nonatomic, strong) UITextField *passworkField;
@property (nonatomic, strong) UIButton *completeBtn;
@property (nonatomic, strong) UIView *hearView;


@end

@implementation RetrievePassworkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initwith];
}

-(void)initwith{
    [self.view addSubview:self.hearView];
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(35*ScaleW, 100*ScaleW, ScreenW - (35*2*ScaleW), 42)];
    image1.image = [UIImage imageNamed:@"et_bg_login.9"];
    [self.view addSubview:image1];
    [self.view addSubview:self.accountFireld];
    UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(35*ScaleW, CGRectGetMaxY(image1.frame)+ 28, ScreenW - (35*2*ScaleW), 42)];
    image2.image = [UIImage imageNamed:@"et_bg_login.9"];
    [self.view addSubview:image2];
    [self.view addSubview:self.passworkField];
    [self.view addSubview:self.completeBtn];
}

#pragma make - UITextFieldDelegater

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    NSLog(@"%@",textField.text);
    if (textField.text.length < 11 && textField.text.length > 0 && textField == self.accountFireld) {
        [self alertShow:@"提示" message:@"请输入正确的手机号码"];
        //        [self showHint:CustomLocalizedString(@"请输入正确的手机号码", nil)];
        
    }else if (textField.text.length < 6 && textField.text.length > 8 &&textField == self.passworkField){
        [self alertShow:@"提示" message:@"密码要大于6位数小于8位数"];
    }
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.accountFireld) {
        
        NSString * text  = [NSString stringWithFormat:@"%@%@",textField.text,string];
        if (text.length == 11) {
            //            self.accountFireld.text = [text substringToIndex:text.length-1];
            //NSLog(@"%@ len:%@",string,textField.text);
            NSString *regex = @"^((13[0-9])|(147)|(17[0,1][6,8])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            BOOL isMatch = [pred evaluateWithObject:text];
            NSLog(@"is:%d",isMatch);
            if (isMatch == false) {
                //                [self showHint:CustomLocalizedString(@"请输入正确的手机号码", nil)];
                [self alertShow:@"提示" message:@"请输入正确的手机号码"];
            }
        }else if (textField.text.length >= 11){
            [self alertShow:@"提示" message:@"号码格式不正确"];
            //            [self showHint:CustomLocalizedString(@"号码格式不正确", nil)];
            
        }
        
    }
    return YES;
}


-(void)alertShow:(NSString *)title message:(NSString *)message{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

-(UIView *)hearView{
    if (!_hearView) {
        _hearView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 64)];
        _hearView.backgroundColor = MainColor;
        UIButton *returnBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 40, 40)];
        [returnBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [returnBtn addTarget:self action:@selector(returnPage) forControlEvents:UIControlEventTouchUpInside];
        [_hearView addSubview:returnBtn];
        UILabel *resetting = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(returnBtn.frame)+5, 24, ScreenW - 90, 40)];
        resetting.text = @"重置密码";
        resetting.textColor = [UIColor whiteColor];
        resetting.textAlignment = NSTextAlignmentCenter;
        [_hearView addSubview:resetting];
    }
    return _hearView;
}

-(UITextField *)accountFireld{
    if (!_accountFireld) {
        _accountFireld = [[UITextField alloc]initWithFrame:CGRectMake(40*ScaleW, 102*ScaleW, ScreenW - (40*2*ScaleW), 40)];
        _accountFireld.placeholder = @"请输入注册的手机号码";
        _accountFireld.delegate = self;
        
    }
    return _accountFireld;
}

-(UITextField *)passworkField{
    if (!_passworkField) {
        _passworkField = [[UITextField alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.accountFireld.frame)+30, ScreenW - (40*2*ScaleW), 40)];
        _passworkField.delegate = self;
        _passworkField.placeholder = @"设置新密码";
    }
    return _passworkField;
}

-(UIButton *)completeBtn{
    if (!_completeBtn) {
        _completeBtn = [[UIButton alloc]initWithFrame:CGRectMake(40*ScaleW, CGRectGetMaxY(self.passworkField.frame)+40, ScreenW - (40*2*ScaleW), 40)];
        [_completeBtn setTitle:@"完成" forState:UIControlStateNormal];
        _completeBtn.backgroundColor = MainColor;
        _completeBtn.layer.cornerRadius = 2.0;
        _completeBtn.layer.masksToBounds = YES;
        [_completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _completeBtn;
}

-(void)completeBtnClick{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",MAIN_URL,FINDPASSWORK_URL];
    NSDictionary *dic = @{@"member_phone":self.accountFireld.text,@"member_pass":self.passworkField.text};
    [NetworkRequestManager postWithUrl:url parameter:dic success:^(id requestData) {
        
        if ([requestData[@"code"] isEqualToString:@"20000"]) {
            [self showHint:@"重置密码失败"];
        }else if ([requestData[@"code"] isEqualToString:@"10000"]){
            [self showHint:@"重置密码成功"];
        }
        
    } failure:^(NSError *error) {
        [self showHint:@"重置密码失败"];
    }];
}

-(void)returnPage{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
    [self.accountFireld resignFirstResponder];
    [self.passworkField resignFirstResponder];
}

@end
